package com.right.health.app.ui.registered;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;

import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.DoctorBean;
import com.right.health.app.bean.FindDoctorResponse;
import com.right.health.app.net.DoctorApi;
import com.right.health.app.ui.registered.adapter.FindDoctorAdapter;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by hopeliao on 2019-09-27
 *
 */
public class FindDoctorActivity extends BasePresenterActivity implements OnHttpListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener {
    private static final int REFRESH_LIST_DODE = 670;
    private static final int LOAD_MORE_CODE = 836;

    private FindDoctorAdapter mAdapter;
    private DoctorApi mDoctorApi;
    private SwipeListRefreshLayout mSwipeListRefreshLayout;
    private List<DoctorBean> mDoctorList;
    private EditText mSearchEt;
    private int mPage;
    private String mKeyword;

    public static void launch(Context context,boolean sendBroadcast) {
        Intent intent = new Intent(context, FindDoctorActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("sendBroadCast", sendBroadcast);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.find_doctor);
    }

    @Override
    public void findView() {
        ListView listView = (ListView) findViewById(R.id.listview);
        mSwipeListRefreshLayout = findViewById(R.id.swiperefresh);
        mSearchEt = findViewById(R.id.et_search);
        mSwipeListRefreshLayout.setOnRefreshListener(this);
        mSwipeListRefreshLayout.setLastItemVisibleListener(this);
        mAdapter = new FindDoctorAdapter(this);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener((parent, view, position, id) -> DoctorDetailActivity.launch(this,mDoctorList.get(position).getDoctorid(), getIntent().getExtras() != null && getIntent().getExtras().getBoolean("sendBroadCast", false)));
        mSearchEt.setOnEditorActionListener((v, actionId, event)->{
                mKeyword = mSearchEt.getText().toString();
                mPage = 1;
                mDoctorApi.addRequestCode(REFRESH_LIST_DODE);
                mDoctorApi.findDoctorList(mPage,mKeyword);
                mSwipeListRefreshLayout.setRefreshing(true);
            mSearchEt.clearFocus();
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(mSearchEt.getWindowToken(), 0);
            return true;
        });

    }

    @Override
    public void initialize() {
        mDoctorApi = new DoctorApi();
        mDoctorApi.setListener(this);
        mSwipeListRefreshLayout.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        mSearchEt.setText(null);
        mKeyword = "";
        mPage = 1;
        mDoctorApi.addRequestCode(REFRESH_LIST_DODE);
        mDoctorApi.findDoctorList(mPage,mKeyword);
    }

    @Override
    public void onLastItemVisible() {
        mDoctorApi.addRequestCode(LOAD_MORE_CODE);
        mDoctorApi.findDoctorList(mPage+1,mKeyword);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            Log.i("TAG", "onResponse: " + response.data);
            FindDoctorResponse doctorResponse = response.cast(FindDoctorResponse.class);
            mPage++;
            if (mDoctorList == null) {
                mDoctorList = new ArrayList<>();
            }
            if (request.requestCode == REFRESH_LIST_DODE) {
                mSwipeListRefreshLayout.onRefreshComplete();
                mDoctorList.clear();
            }
            if (doctorResponse.page != null) {
                mSwipeListRefreshLayout.setCanLoadMore(!doctorResponse.page.lastPage);
            }
            if (!CollectionUtil.isEmpty(doctorResponse.doctors)) {
                mDoctorList.addAll(doctorResponse.doctors);
            }
            mAdapter.setDataSource(mDoctorList);
        }
    }
}

